from threading import Thread
import threading


num = 0
def work1(n):
    global num
    for i in range(n):
        num+=1
    print('work1---%d'%num)
def work2(n):
    global num
    for i in range(n):
        num+=1
    print('work2---%d'%num)

class MyThread(Thread):
    """多线程封装，继承threading.Thread，如何重写run方法即可，方法内容自行定义，name保存线程名字"""
    def run(self):
        mag = self.name
        print(mag)

if __name__ == '__main__':

    p1= Thread(target=work1,args=(10000000,))
    p2= Thread(target=work2,args=(10000000,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    print(num)
    #创建锁
    mex = threading.Lock()
    #锁上
    mex.acquire()
    #解锁
    mex.release()
    #acquire会阻塞，当两个人同时对已经锁上的锁进行时会死锁
    #没锁上时acquier不会阻塞